#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include <QFile>
#include "paths.h"
#include <QFileDialog>
#include "QMessageBox"
SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);
    setFileH=SetFileHandler::getInstance();
    this->setupDialog();

}

SettingsDialog::~SettingsDialog()
{
    delete ui;
}

void SettingsDialog::closeEvent(QCloseEvent*e){
    this->QDialog::closeEvent(e);
    delete this;
}


void SettingsDialog::on_okButton_clicked(){

        if(ui->ffoxCustomRadio->isChecked()){
         if( !setFileH->writeFfoxCustomPath(ui->ffoxCustomPathLabel->text())){
             QString error= setFileH->getLastError();
             QMessageBox msg;
             msg.setWindowTitle("Error");
             msg.setText(error);
             msg.exec();
             return;
         }
        }else{
            if(!setFileH->writeFfoxCustomPath("")){
                QString error= setFileH->getLastError();
                QMessageBox msg;
                msg.setWindowTitle("Error");
                msg.setText(error);
                msg.exec();
                return;
            }
        }

        if(ui->chroCustomRadio->isChecked()){
         if( !setFileH->writeChroCustomPath(ui->chroCustomPathLabel->text())){
             QString error= setFileH->getLastError();
             QMessageBox msg;
             msg.setWindowTitle("Error");
             msg.setText(error);
             msg.exec();
             return;
         }
        }else{
            if(!setFileH->writeChroCustomPath("")){
                QString error= setFileH->getLastError();
                QMessageBox msg;
                msg.setWindowTitle("Error");
                msg.setText(error);
                msg.exec();
                return;
            }
        }

        if(ui->operaCustomRadio->isChecked()){
         if(! setFileH->writeOperaCustomPath(ui->operaCustomPathLabel->text())){
             QString error= setFileH->getLastError();
             QMessageBox msg;
             msg.setWindowTitle("Error");
             msg.setText(error);
             msg.exec();
             return;
         }
        }else{
            if(!setFileH->writeOperaCustomPath("")){
                QString error= setFileH->getLastError();
                QMessageBox msg;
                msg.setWindowTitle("Error");
                msg.setText(error);
                msg.exec();
                return;
            }
        }


    this->close();


}


void SettingsDialog::on_ffoxDefRadio_clicked(){
    ui->ffoxBrowse->setDisabled(true);
}

void SettingsDialog::on_chroDefRadio_clicked(){
    ui->chroBrowse->setDisabled(true);
}

void SettingsDialog::on_operaDefRadio_clicked(){
    ui->operaBrowse->setDisabled(true);
}

void SettingsDialog::on_ffoxCustomRadio_clicked(){
    ui->ffoxBrowse->setDisabled(false);
}

void SettingsDialog::on_chroCustomRadio_clicked(){
    ui->chroBrowse->setDisabled(false);
}

void SettingsDialog::on_operaCustomRadio_clicked(){
    ui->operaBrowse->setDisabled(false);
}

void SettingsDialog::on_ffoxBrowse_clicked(){
    QString  fileName = QFileDialog::getExistingDirectory(this, tr("Select Firefox installation folder"),
                                                          "C:/",
                                                          QFileDialog::ShowDirsOnly
                                                          | QFileDialog::DontResolveSymlinks);
    ui->ffoxCustomPathLabel->setText(fileName);
}

void SettingsDialog::on_chroBrowse_clicked(){
    QString  fileName = QFileDialog::getExistingDirectory(this, tr("Select Chromium installation folder"),
                                                          "C:/",
                                                          QFileDialog::ShowDirsOnly
                                                          | QFileDialog::DontResolveSymlinks);
    ui->chroCustomPathLabel->setText(fileName);
}

void SettingsDialog::on_operaBrowse_clicked(){
    QString  fileName = QFileDialog::getExistingDirectory(this, tr("Select Opera installation folder"),
                                                          "C:/",
                                                          QFileDialog::ShowDirsOnly
                                                          | QFileDialog::DontResolveSymlinks);
    ui->operaCustomPathLabel->setText(fileName);
}


void SettingsDialog::setupDialog(){

    QString ffoxCustomPath;
    QString operaCustomPath;
    QString chroCustomPath;
    if(setFileH->readFfoxCustomPath(&ffoxCustomPath)){
        if(ffoxCustomPath.isEmpty())ui->ffoxDefRadio->setChecked(true);
        else {
            ui->ffoxCustomRadio->setChecked(true);
            ui->ffoxCustomPathLabel->setText(ffoxCustomPath);
            ui->ffoxBrowse->setEnabled(true);
        }
    }else{
        QString error= setFileH->getLastError();
        QMessageBox msg;
        msg.setWindowTitle("Error");
        msg.setText(error);
        msg.exec();
        return;

    }
    if(setFileH->readOperaCustomPath(&operaCustomPath)){
        if(operaCustomPath.isEmpty())ui->operaDefRadio->setChecked(true);
        else {
            ui->operaCustomRadio->setChecked(true);
            ui->operaCustomPathLabel->setText(operaCustomPath);
            ui->operaBrowse->setEnabled(true);
        }
    }else{
        QString error= setFileH->getLastError();
        QMessageBox msg;
        msg.setWindowTitle("Error");
        msg.setText(error);
        msg.exec();
        return;

    }
    if(setFileH->readChroCustomPath(&chroCustomPath)){
        if(chroCustomPath.isEmpty())ui->chroDefRadio->setChecked(true);
        else {
            ui->chroCustomRadio->setChecked(true);
            ui->chroCustomPathLabel->setText(chroCustomPath);
            ui->chroBrowse->setEnabled(true);
        }
    }else{
        QString error= setFileH->getLastError();
        QMessageBox msg;
        msg.setWindowTitle("Error");
        msg.setText(error);
        msg.exec();
        return;

    }


}
